<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Home extends Controller_Admin {

    public function before()
    {
        parent::before();
        
        if ($this->auto_render === TRUE)
        {
            $this->title = '管理后台';
            $this->navigate->add('管理后台');
        }
    }

    public function after()
    {
        if ($this->auto_render === TRUE)
        {
        	;
        }
        
        parent::after();
    }
    
    public function action_index(){
    	lib_admin::redirect('home/adda');
    }
    
    //文章
	public function action_adda()
	{
		$this->template->add_js(array(
			'media/js/admin/a.js'
		));
		
		if (($post = $this->post) && lib_validate::need($post,array('title','content'))){
			MON::factory('article')->insert(array(
				'title'=>$post['title'],        
			    'rewrite'=>project::make_rewrite($post['rewrite']),
			    'keywords'=>isset($post['keywords'])?$post['keywords']:'',
			    'description'=>isset($post['description'])?$post['description']:'',
			    'content'=>$post['content'],    
			    'addTime'=>time()
			));
			
			if (strlen($post['tags'])){
				$article_id = MON::inser_id();
				$tags = common::explode(',',$post['tags']);
				foreach ($tags as $tag){
					if (!$o = MON::factory('tag')->findOne(array('tag'=>$tag)))
						$o = MON::factory('tag')->insert(array(
		                    'tag'=>$tag,   
		                    'addTime'=>time()
		                ));
		            MON::factory('tag_article')->insert(array(
						'article_id'=>new MongoId($article_id),
						'tag_id'=>new MongoId($o['_id'])
					));
				}
			}
			
			lib_message::message('文章已保存',true);
		}
		
		$this->content = lib_view::factory('adda')
		->set('tags',MON::factory('tag')->find())
		->set('xheditor',m::factory('xheditor'));
	}
	
	public function action_edita()
	{		
		$this->template->add_js(array(
			'media/js/admin/a.js'
		));
		
		if ($_id = $this->request->param('id')){
			if ($a = MON::factory('article',$_id)){
				if (($post = $this->post) && lib_validate::need($post,array('title','content'))){
					MON::factory('article')->update(array('_id'=>new MongoId($_id)),array('$set'=>array(
						'title'=>$post['title'],
					    'rewrite'=>project::make_rewrite($post['rewrite'],$_id),
					    'keywords'=>isset($post['keywords'])?$post['keywords']:'',
					    'description'=>isset($post['description'])?$post['description']:'',
					    'content'=>$post['content'],
					    'updateTime'=>time()
					)));
					
					MON::factory('tag_article')->remove(array('article_id' => new MongoId($_id)));
					
					if (strlen($post['tags'])){
						$tags = common::explode(',',$post['tags']);
						foreach ($tags as $tag){
							if (!$o = MON::factory('tag')->findOne(array('tag'=>$tag)))
								$o = MON::factory('tag')->insert(array(
				                    'tag'=>$tag,   
				                    'addTime'=>time()
				                ));
				            MON::factory('tag_article')->insert(array(
								'article_id'=>new MongoId($_id),
								'tag_id'=>new MongoId($o['_id'])
							));
						}
					}
					
					lib_message::message('文章已保存',true);
					lib_url::redirect($this->referer);
				}
				
				$this->content = lib_view::factory('edita')
				->set('a',$a)
				->set('tags',Model::factory('tag')->get_tags_text($_id))
				->set('xheditor',m::factory('xheditor',$a));
			}else{
				lib_message::message('文章不存在');
				lib_url::redirect($this->referer);
			}
		}
	}
	
	public function action_dela(){
		if ($_id = $this->request->param('id')){
			if (MON::factory('article',$_id)){
				MON::factory('article')->remove(array('_id' => new MongoId($_id)), array("justOne" => true));
				MON::factory('tag_article')->remove(array('article_id' => new MongoId($_id)));
				lib_message::message('文章已删除');
			}
		}elseif (($post = $this->post) && lib_validate::need($post,array('article_id'))){
			$article_ids = array();
			foreach ($post['article_id'] as $article_id){
				$article_ids[] = new MongoId($article_id);
			}
			MON::factory('article')->remove(array('_id'=>array('$in'=>$article_ids)));
			MON::factory('tag_article')->remove(array('article_id' =>array('$in'=>$article_ids)));
			lib_message::message('文章已删除');
		}
		lib_url::redirect($this->referer);
	}
	
	public function action_lista(){
		$list = MON::factory('article');
		
		$filter = lib_filter::instance('article',2);
		
		if ($filter->filtering()){
			$list = $filter->filter($list);
		}else{
			$list = $list->find()->sort(array('addTime'=>-1));
		}
		
		$this->pagination = new Pagination(array(
			'total_items'=>$list->count()
		));
		
		$list
		->skip($this->pagination->offset)
		->limit($this->pagination->items_per_page);
		
		$this->content = lib_view::factory('lista')
		->set('list',$list)->set('filter',$filter->render());
	}
	//--end--文章
	
	//评论
	public function action_commentlist(){
		$list = MON::factory('comment')->find();
		
		$this->content = lib_view::factory('commentlist')
		->set('list',$list);
	}
	
	public function action_commentdel(){
		if ($_id = $this->request->param('id')){
			if (MON::factory('comment',$_id)){
				MON::factory('comment')->remove(array('_id' => new MongoId($_id)), array("justOne" => true));
				lib_message::message('评论已删除');
			}
		}elseif (($post = $this->post) && lib_validate::need($post,array('comment_id'))){
			$comment_ids = array();
			foreach ($post['comment_id'] as $comment_id){
				$comment_ids[] = new MongoId($comment_id);
			}
			MON::factory('comment')->remove(array('_id'=>array('$in'=>$comment_ids)));
			lib_message::message('评论已删除');
		}
		lib_url::redirect($this->referer);
	}
	//--end--评论
	
	//标签
	public function action_taglist(){
		$list = MON::factory('tag')->find()->sort(array('addTime'=>-1));
		
		$this->pagination = new Pagination(array(
			'total_items'=>$list->count()
		));
		
		$list
		->skip($this->pagination->offset)
		->limit($this->pagination->items_per_page);
		
		$this->content = lib_view::factory('taglist')
		->set('list',$list);
	}
	
	public function action_tagedit(){
        $this->content = lib_view::factory('tagedit');
        
        if ($_id = $this->request->param('id')){
            if (($post = $this->post) && lib_validate::need($post,array('tag'))){
            	if (MON::factory('tag')->findOne(array('tag'=>$post['tag']))){
            		lib_message::message('标签已存在',true);
            	}else{
            		MON::factory('tag')->update(array('_id'=>new MongoId($_id)),array('$set'=>array(
						'tag'=>$post['tag'],
						'updateTime'=>time()
					)));
					lib_message::message('标签已修改',true);
            	}
            }
            $this->content->set('tag',MON::factory('tag',$_id));
        }else{
            if (($post = $this->post) && lib_validate::need($post,array('tag'))){
            	if (MON::factory('tag')->findOne(array('tag'=>$post['tag']))){
            		lib_message::message('标签已存在',true);
            	}else{
            		MON::factory('tag')->insert(array(
	                    'tag'=>$post['tag'],   
	                    'addTime'=>time()
	                ));
	                lib_message::message('标签已保存',true);
            	}
            }
        }
	}
	
	public function action_tagdel(){
		if ($_id = $this->request->param('id')){
			if (MON::factory('tag',$_id)){
				MON::factory('tag')->remove(array('_id' => new MongoId($_id)), array("justOne" => true));
				MON::factory('tag_article')->remove(array('tag_id' => new MongoId($_id)));
				lib_message::message('标签已删除');
			}
		}elseif (($post = $this->post) && lib_validate::need($post,array('tag_id'))){
			$tag_ids = array();
			foreach ($post['tag_id'] as $tag_id){
				$tag_ids[] = new MongoId($tag_id);
			}
			MON::factory('tag')->remove(array('_id'=>array('$in'=>$tag_ids)));
			MON::factory('tag_article')->remove(array('tag_id' =>array('$in'=>$tag_ids)));
			lib_message::message('标签已删除');
		}
		lib_url::redirect($this->referer);
	}
	//--end--标签
} // End Welcome
